package org.jeecg.modules.qy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhyd.oauth.utils.UuidUtils;
import org.jeecg.common.util.NumberUtils;
import org.jeecg.common.util.oss.OssBootUtil;
import org.jeecg.modules.qy.entity.QyCommodity;
import org.jeecg.modules.qy.entity.QySourceCodeCreate;
import org.jeecg.modules.qy.entity.QySourceCodeCreateList;
import org.jeecg.modules.qy.mapper.QySourceCodeCreateListMapper;
import org.jeecg.modules.qy.service.IQyCommodityService;
import org.jeecg.modules.qy.service.IQySourceCodeCreateListService;
import org.jeecg.modules.qy.service.IQySourceCodeCreateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 溯源码生成表
 * @Author: jeecg-boot
 * @Date:   2021-03-15
 * @Version: V1.0
 */
@Service
public class QySourceCodeCreateListServiceImpl extends ServiceImpl<QySourceCodeCreateListMapper, QySourceCodeCreateList> implements IQySourceCodeCreateListService {



    @Autowired
    private IQyCommodityService commodityService;

    @Autowired
    private IQySourceCodeCreateService codeCreateService;

    @Override
    public void saveCodeList(QySourceCodeCreate sourceCode, QyCommodity commodity){
        List<QySourceCodeCreateList> tempList = new ArrayList<>();
        int len = String.valueOf(sourceCode.getCodeNum()).length();
        QySourceCodeCreateList codeList;
        for(int a=0;a<sourceCode.getCodeNum();a++){
            try{
                String codeNo = sourceCode.getBatchNo()+ NumberUtils.autoGenericCode(String.valueOf(a),6);
                codeList= new QySourceCodeCreateList();
                codeList.setCodeNo(codeNo);
                codeList.setBatchNo(sourceCode.getBatchNo());
                codeList.setCodeCreateId(sourceCode.getId());
                codeList.setDownloadTimes(0);
                codeList.setCommodityId(commodity.getId());
                codeList.setId(UuidUtils.getUUID());
                codeList.setCodeUrl(OssBootUtil.createQrCode("https://api.xiaomcc.com/#/home?codeId="+codeList.getId(),sourceCode.getId(),codeList.getCodeNo()));
                tempList.add(codeList);
            }catch (Exception e){
                log.error("生成码错误：错误批次号："+sourceCode.getBatchNo()+",错误生成ID："+sourceCode.getId());
                e.printStackTrace();
            }
        }

        saveBatch(tempList);

        //修改权益
        commodityService.updateById(commodity);

        /*//处理完code保存之后修改生成状态
        sourceCode.setStatus(1);
        updateById(sourceCode);*/
    }


    @Async("asyncExecutor")
    @Override
    public void saveAsyncCodeList(QySourceCodeCreate sourceCode,QyCommodity commodity){
        List<QySourceCodeCreateList> tempList = new ArrayList<>();
        int len = String.valueOf(sourceCode.getCodeNum()).length();
        QySourceCodeCreateList codeList;
        for(int a=0;a<sourceCode.getCodeNum();a++){
            try{
                String codeNo = sourceCode.getBatchNo()+NumberUtils.autoGenericCode(String.valueOf(a),6);
                codeList= new QySourceCodeCreateList();
                codeList.setCodeNo(codeNo);
                codeList.setDownloadTimes(0);
                codeList.setBatchNo(sourceCode.getBatchNo());
                codeList.setCodeCreateId(sourceCode.getId());
                codeList.setCommodityId(commodity.getId());

                codeList.setId(UuidUtils.getUUID());
                codeList.setCodeUrl(OssBootUtil.createQrCode("https://h5.sumtrace.com/#/home?codeId="+codeList.getId(),sourceCode.getId(),codeList.getCodeNo()));

                tempList.add(codeList);
            }catch (Exception e){
                e.printStackTrace();
                log.error("生成码错误：错误批次号："+sourceCode.getBatchNo()+",错误生成ID："+sourceCode.getId());
            }
        }

        saveBatch(tempList);

        //修改权益
        commodityService.updateById(commodity);

        //处理完code保存之后修改生成状态
        sourceCode.setStatus(1);
        codeCreateService.updateById(sourceCode);
    }
}
